package com.xy.controller.calculate.lirun.temu;

import com.xy.api.model.dto.calculate.lirun.temu.TemuLirunProductFormDTO;
import com.xy.api.model.request.calculate.lirun.temu.SaveTemuLirunProductFormRequest;
import com.xy.api.model.request.calculate.lirun.temu.TemuLirunProductRequest;
import com.xy.api.model.response.R;
import com.xy.api.model.response.Rows;
import com.xy.api.service.calculate.lirun.temu.ITemuLirunProductInstanceService;
import com.xy.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/calculate/lirun/temu/instance")
public class TemuLirunProductInstanceController  extends BaseController {

    @Autowired
    private ITemuLirunProductInstanceService temuLirunProductInstanceService;

    @PostMapping("/list")
    public R queryTemuLirunInstanceList(@RequestBody TemuLirunProductRequest request) {
        List<TemuLirunProductFormDTO> list = temuLirunProductInstanceService.queryTemuLirunInstanceList(request.getId());
        Rows<TemuLirunProductFormDTO> rows = new Rows<>();
        rows.setRows(list);
        rows.setTotal(list.size());
        return success(rows);
    }

    @PostMapping("/save")
    public R saveTemuLirunForm(@RequestBody SaveTemuLirunProductFormRequest request) {
        temuLirunProductInstanceService.saveTemuLirunForm(request);
        return success();
    }

    @PostMapping("/delete")
    public R deleteTemuLirunForm(@RequestBody  TemuLirunProductFormDTO request) {
        temuLirunProductInstanceService.deleteTemuLirunForm(request.getId());
        return success();
    }

    @PostMapping("/addForm")
    public R addForm(@RequestBody TemuLirunProductFormDTO request) {
        TemuLirunProductFormDTO form = temuLirunProductInstanceService.addForm(request);
        return success(form);
    }

    @PostMapping("/calculate")
    public R calculateTemuLirunForm(@RequestBody SaveTemuLirunProductFormRequest request) {
        temuLirunProductInstanceService.saveTemuLirunForm(request);
        temuLirunProductInstanceService.calculateTemuLirunForm(request.getTemuLirunProductId());
        return success();
    }
}
